package net.wanho.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.wanho.dto.LoginUserDTO;
import net.wanho.entity.User;
import net.wanho.service.UserService;
import net.wanho.vo.R;
import net.wanho.vo.UserVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2025-09-19 10:31
 * Description：<描述>
 */
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/register")
    public R register(@RequestBody User user) {
        userService.save(user);
        return R.success("注册成功", user);
    }

    @GetMapping
    public R getAll(){
        List<User> list = userService.list();
        return R.success(list);
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return R.success(user);
    }

    @PutMapping
    public R update(@RequestBody User user) {
        userService.updateById(user);
        return R.success("更新成功");
    }

    @PostMapping("/login")
    public R login(@RequestBody LoginUserDTO loginUserDTO){
        UserVo userVo = userService.login(loginUserDTO);
        return R.success(userVo);
    }

    @GetMapping("/page")
    public R findByPage(@RequestParam(required = false, defaultValue = "1") int pageNum,
                        @RequestParam(required = false, defaultValue = "5") int pageSize){
        Page<User> page = new Page<>(pageNum, pageSize);
        Page<User> p = userService.page(page);
        System.out.println(page == p);

        return R.success(page);
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        boolean b = userService.removeById(id);
        return R.success("删除成功");
    }
}
